#include <iostream>
#include "include/Monde.h"
#include "include/Continent.h"
#include "include/Territoire.h"
#include "include/Visitor.h"
#include "include/Jeu.h"

using namespace std;


Monde::~Monde()
{
   //printf("Destruction de Monde!\n");
   vector<Continent*>::const_iterator continents_ite;
   for(continents_ite = getContinents()->begin();
       continents_ite != getContinents()->end();
       continents_ite++)
      delete *continents_ite;
}

void Monde::accept(Visitor * _visitor)
{
   _visitor->visit(this);
}

int Monde::countTerritoires() const
{
   return indexedTerritoires.size();
}

int Monde::countTerritoiresJoueur(Joueur* _joueur)
{
   int result = 0;
   vector<Territoire*>::const_iterator territoires_ite;
   for(territoires_ite = getIndexedTerritoires()->begin();
       territoires_ite != getIndexedTerritoires()->end();
       territoires_ite++)
   {
      if ( (*territoires_ite)->appartientAuJoueur(_joueur) )
         result++;
   }
   return result;
}

Continent * Monde::addContinent(const char * _nom)
{
   continents.push_back( new Continent(this, _nom) );
   return continents.back();
}

Continent* Monde::getContinentByName(const char* _name)
{
   vector<Continent*>::iterator continents_ite;
   for(continents_ite = getContinents()->begin();
       continents_ite != getContinents()->end();
       continents_ite++)
   {
      if ( strcmp(_name, (*continents_ite)->getNom()) == 0 )
         return *continents_ite;
   }
   return NULL;
}

Territoire* Monde::getTerritoireByName(const char * _name)
{
   Territoire * ret;
   vector<Continent*>::const_iterator continents_ite;
   for(continents_ite = getContinents()->begin();
       continents_ite != getContinents()->end();
       continents_ite++)
   {      
      if( (ret = (*continents_ite)->getTerritoireByName(_name)) != NULL)
         return ret;
   }
   return NULL;
}

ostream & operator << (ostream & out, const Monde & _monde) 
{ 
   out << "Le monde: " << endl;

   vector<Continent*>::const_iterator continents_ite;
   for(continents_ite = _monde.getContinents()->begin();
       continents_ite != _monde.getContinents()->end();
       continents_ite++)
   {
      out << **continents_ite << endl;
   }
   
   return out;
}

